<template>

  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="人员姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入人员姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组长姓名" prop="leader">
        <el-input
          v-model="queryParams.leader"
          placeholder="请输入组长姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="sum_indicator_type">
        <el-radio v-model="queryParams.sumIndicatorType" label="1" @change="handleQuery">消耗小时数</el-radio>
        <el-radio v-model="queryParams.sumIndicatorType" label="2" @change="handleQuery">最终得分</el-radio>
      </el-form-item>
      <el-form-item label="任务时间" v-if="activeTab=='day'">
        <el-date-picker
          v-model="dayDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="任务时间" v-if="activeTab=='month'">
        <el-date-picker
          v-model="monthDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="任务时间" v-if="activeTab=='year'">
        <el-date-picker
          v-model="yearDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeTab" @tab-click="clickTab">
      <el-tab-pane label="按天统计" name="day">
        <div style="height: 70vh; overflow: auto;">
          <el-table ref="dayTable" :data="dayDataMapList" height="100%" stripe border
                    :row-class-name="tableRowClassName"
                    @header-dragend="handleTableResize">
            <el-table-column label="序号" align="right" type="index" width="50" fixed/>
            <el-table-column v-for="column in dayColumnNameList" :key="column.code" :prop="column.code" sortable
                             :fixed="column.code=='username'" :label="column.name" width="200">
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane>
      <el-tab-pane label="按月统计" name="month">
        <div style="height: 70vh; overflow: auto;">
          <el-table ref="monthTable" :data="monthDataMapList" height="100%" stripe border
                    :row-class-name="tableRowClassName"
                    @header-dragend="handleTableResize">
            <el-table-column label="序号" align="right" type="index" width="50" fixed/>
            <el-table-column v-for="column in monthColumnNameList" :key="column.code" :prop="column.code" sortable
                             :fixed="column.code=='username'" :label="column.name" width="200">
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane>
      <el-tab-pane label="按年统计" name="year">
        <div style="height: 70vh; overflow: auto;">
          <el-table ref="yearTable" :data="yearDataMapList" height="100%" stripe border
                    :row-class-name="tableRowClassName"
                    @header-dragend="handleTableResize">
            <el-table-column label="序号" align="right" type="index" width="50" fixed/>
            <el-table-column v-for="column in yearColumnNameList" :key="column.code" :prop="column.code" sortable
                             :fixed="column.code=='username'" :label="column.name" width="200">
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {getStatisticsList, listTask} from "@/api/system/task";

export default {
  name: "Task",
  dicts: ['approve_type'],
  data() {
    return {
      activeTab: 'day', // 设置默认激活的标签页
      // 查询参数
      queryParams: {
        username: undefined,
        leader: undefined,
        sumIndicatorType: "1",
        approveType: undefined,
        pattern: "yyyy-MM-dd"
      },
      dayDataMapList: [],
      dayColumnNameList: [],
      monthDataMapList: [],
      monthColumnNameList: [],
      yearDataMapList: [],
      yearColumnNameList: [],
      // 日期范围
      dayDateRange: this.getDayDefaultCurrentTime(),
      monthDateRange: this.getMonthDefaultCurrentTime(),
      yearDateRange: this.getYearDefaultCurrentTime(),
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleTableResize() {
      this.$nextTick(() => {
        if (this.activeTab === 'day') {
          this.$refs.dayTable.doLayout();
        } else if (this.activeTab === 'month') {
          this.$refs.monthTable.doLayout();
        } else if (this.activeTab === 'year') {
          this.$refs.yearTable.doLayout();
        }
      });
    },
    tableRowClassName({row, rowIndex}) {
      return rowIndex % 2 === 0 ? 'even-row' : 'odd-row';
    },
    clickTab() {
      if (this.activeTab === 'day') {
        // if (this.dayDataMapList.length == 0) {
        this.getList();
        // }
      } else if (this.activeTab === 'month') {
        // if (this.monthDataMapList.length == 0) {
        this.getList();
        // }
      } else if (this.activeTab === 'year') {
        // if (this.yearDataMapList.length == 0) {
        this.getList();
        // }
      }
    },
    getDayDefaultCurrentTime() {
      const currentDate = new Date();
      // 获取当前日期的年、月和日
      const currentYear = currentDate.getFullYear();
      const currentMonth = currentDate.getMonth() + 1;
      const currentDay = currentDate.getDate();
      // 当前起止时间
      const dayStartTime = `${currentYear}-${currentMonth < 10 ? '0' + currentMonth : currentMonth}-01 00:00:00`;
      const dayEndTime = `${currentYear}-${currentMonth < 10 ? '0' + currentMonth : currentMonth}-${currentDay < 10 ? '0' + currentDay : currentDay} 00:00:00`;
      return [dayStartTime, dayEndTime];
    },
    getMonthDefaultCurrentTime() {
      const currentDate = new Date();
      // 获取当前日期的年、月和日
      const currentYear = currentDate.getFullYear();
      const currentMonth = currentDate.getMonth() + 1;
      const currentDay = currentDate.getDate();
      // 当前起止时间
      const monthStartTime = `${currentYear}-01-01 00:00:00`;
      const dayEndTime = `${currentYear}-${currentMonth < 10 ? '0' + currentMonth : currentMonth}-${currentDay < 10 ? '0' + currentDay : currentDay} 00:00:00`;
      return [monthStartTime, dayEndTime];
    },
    getYearDefaultCurrentTime() {
      const currentDate = new Date();
      // 获取当前日期的年、月和日
      const currentYear = currentDate.getFullYear();
      const currentMonth = currentDate.getMonth() + 1;
      const currentDay = currentDate.getDate();
      // 当前起止时间
      const yearStartTime = `${currentYear - 1}-01-01 00:00:00`;
      const dayEndTime = `${currentYear}-${currentMonth < 10 ? '0' + currentMonth : currentMonth}-${currentDay < 10 ? '0' + currentDay : currentDay} 00:00:00`;
      return [yearStartTime, dayEndTime];
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      let dateRange = [];
      if (this.activeTab === 'day') {
        this.queryParams.pattern = "yyyy-MM-dd";
        dateRange = this.dayDateRange;
      } else if (this.activeTab === 'month') {
        this.queryParams.pattern = "yyyy-MM";
        dateRange = this.monthDateRange;
      } else if (this.activeTab === 'year') {
        this.queryParams.pattern = "yyyy";
        dateRange = this.yearDateRange;
      }
      getStatisticsList(this.addDateRange(this.queryParams, dateRange)).then(response => {
        if (this.activeTab === 'day') {
          this.dayColumnNameList = response.data.columnNameList;
          this.dayDataMapList = response.data.dataMapList;
        } else if (this.activeTab === 'month') {
          this.monthColumnNameList = response.data.columnNameList;
          this.monthDataMapList = response.data.dataMapList;
        } else if (this.activeTab === 'year') {
          this.yearColumnNameList = response.data.columnNameList;
          this.yearDataMapList = response.data.dataMapList;
        }
        // 当设置有固定列的时候，延迟执行重新计算表格高度
        this.$nextTick(() => {
          let table = null;
          if (this.activeTab === 'day') {
            table = this.$refs.dayTable;
          } else if (this.activeTab === 'month') {
            table = this.$refs.monthTable;
          } else if (this.activeTab === 'year') {
            table = this.$refs.yearTable;
          }
          if (table) {
            table.$nextTick(() => {
              table.doLayout();
            });
          }
        });
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dayDateRange = this.getDayDefaultCurrentTime()
      this.monthDateRange = this.getMonthDefaultCurrentTime()
      this.yearDateRange = this.getYearDefaultCurrentTime()
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
}
;
</script>


<style>
.el-table .even-row {
  background-color: #f0f0f0; /* 设置偶数行的背景色 */
}

.el-table .odd-row {
  background-color: #ffffff; /* 设置奇数行的背景色 */
}

.el-table .el-table__row--current {
  background-color: #e0f3ff; /* 设置选中行的背景色 */
}

/* 增加 el-table 的滚动条宽度 */
/*.el-table__body-wrapper::-webkit-scrollbar {
  height: 10px; !* 设置横向滚动条高度 *!
  width: 10px; !* 设置横向滚动条宽度 *!
}*/

</style>
